package com.tony.akka.sample3;

import akka.actor.*;
import akka.japi.pf.DeciderBuilder;
import scala.concurrent.duration.Duration;

import static akka.actor.SupervisorStrategy.escalate;

/**
 * @version V1.0  </br>
 * @ClassName Supervisor </br>
 * @Description TODO  </br>
 * @Author tonywang</br>
 * @DATE 3/19/2019 10:50 AM</br>
 */
public class Supervisor extends AbstractLoggingActor {

    public static final OneForOneStrategy STRATEGY = new OneForOneStrategy(
            10,
            Duration.create("10 seconds"),
            DeciderBuilder
                    .match(RuntimeException.class, ex -> escalate())
                    .build()
    );

    @Override
    public Receive createReceive() {
        final ActorRef child = getContext().actorOf(NonTrustWorthyChild.props(), "child");
        return receiveBuilder().matchAny(any-> child.forward(any, getContext())).build();
    }

    @Override
    public SupervisorStrategy supervisorStrategy() {
        return STRATEGY;
    }

    public static Props props() {
        return Props.create(Supervisor.class);
    }
}
